<?php
namespace core\log;
/**
 * 对抛出的异常进行处理记录
 */
class ExceptionHandle{
    
    
    public static function instance(){
        static $instance;
        if(!$instance){
            $instance = new ExceptionHandle();
        }
        return $instance;
    }
    
    /**
     * 添加一个异常对象 做完日志处理
     */
    public function report(\Exception $exception){
     //   pr($exception);exit;
        $message = $this->getSourceCode($exception);
        return $message;
    }
    
    
    
    
    /**
     * 检查过滤写入的日志信息
     */
    private function getSourceCode($exception){
        $preException = $exception->getPrevious();
        $msgList = $exception->getTrace();
        if( $preException ){            
            $preMessage = $this->getSourceCode($preException);
            unset($msgList[0]['args'][2]); 
            $lastMsg = [$msgList[0]];
            $lastMsg[0]['last'] = 1;
            $ss = array_merge($lastMsg,$preMessage);
            return $ss;
        }          
        $delFiles = ['\library\think\Container.php'];
        $exceptionLog = [];// [ [ 'excption' => get_class($exception)]];
        
        foreach( $msgList as $key => $val ){
            $result = explode('thinkphp',$val['file']);
            if( in_array( $result[1], $delFiles)) break;
            $exceptionLog[] = $val;
        }
        unset($msgList);
        unset($exception);
        return $exceptionLog;
        
    }
    
    
    
    
}